package com.greenant.store.view;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.bean.AntGoods;
import com.greenant.store.bean.AntGoodsStyles;
import com.greenant.store.bean.AntGoodsStylesBase;
import com.greenant.store.bean.GoodsKind;
import com.greenant.store.bean.GoodsKindBase;
import com.greenant.store.service.RetrieveDataAsyn;
import com.greenant.store.service.RetrieveDataListener;
import com.greenant.store.utils.UtilTools;
import com.greenant.store.view.MSpinner.MSpinnerBean;
import com.greenant.store.view.MSpinner.OnMSpinnerItemClickListener;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GoodsHandleDialog extends Dialog implements android.view.View.OnClickListener, OnMSpinnerItemClickListener {
	private Context mContext;
	private AntGoods mAntGoods;
	private List<MSpinnerBean> mMSpinnerBeanListKind = new ArrayList<MSpinnerBean>();
	private List<MSpinnerBean> mMSpinnerBeanListStyle = new ArrayList<MSpinnerBean>();
	private List<GoodsKindBase> mGoodsKindBaseList = new ArrayList<GoodsKindBase>();
	private GoodsKind mCurrentGoodsKind;
	private AntGoodsStyles mCurrentGoodsStyles;
	
	private ImageView mIvImage;
	private Button mBtnLeft;
	private Button mBtnMiddle;
	private Button mBtnRight;
	private TextView mTvPromptShelf;
	private TextView mTvGoodsTitle;
	private TextView mTvGoodsDes;
	private EditText mEtPricePurchase;
	private EditText mEtPriceSell;
	private EditText mEtDiscount;
	private EditText mEtPriceByDiscount;
	private EditText mEtStore;
	private EditText mEtSpecs;
	private EditText mEtUnit;
	private EditText mEtWeightUnit;
	private EditText mEtWeightStep;
	private View mViewRecommend;
	private View mViewWeightUnit;
	private View mViewWeightStep;
	private CheckBox mCbWeightableYes;
	private CheckBox mCbWeightableNo;
	private MSpinner mMSpinnerKind;
	private MSpinner mMSpinnerStyle;
	
	private DecimalFormat mDecimalFormat;
	
	private double mOriginPricePurchase;
	private double mOriginPriceSell;
	private double mOriginDiscount;
	private double mOriginStore;
	private double mOriginWeightStep;
	private boolean mOriginWeightable;
	private boolean mOriginRecommend;
	private String mOriginSpecs;
	private String mOriginUnit;
	private String mOriginWeightUnit;
	
	private double mTempPricePurchase;
	private double mTempPriceSell;
	private double mTempDiscount;
	private double mTempStore;
	private double mTempWeightStep;
	private boolean mTempWeightable;
	private boolean mTempRecommend;
//	private String mTempSpecs;
//	private String mTempUnit;
//	private String mTempWeightUnit;
	
	private TextWatcher mTextWatcherForPricePurchase = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			try{
				double pricePurchase = Double.valueOf(s.toString());
				if(pricePurchase < 0) {
					mEtPricePurchase.setText(String.valueOf(Math.abs(pricePurchase)));
				}
				else {
					mTempPricePurchase = pricePurchase;
				}
			}
			catch(Exception e) {
				mTempPricePurchase = 0;
				e.printStackTrace();
			}
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	
	private TextWatcher mTextWatcherForPriceSell = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			try{
				double priceSell = Double.valueOf(s.toString());
				if(priceSell < 0) {
					mEtPriceSell.setText(String.valueOf(Math.abs(priceSell)));
				}
				else {
					mTempPriceSell = priceSell;
					
					if(mTempDiscount > 0) {
						mEtPriceByDiscount.setText(mDecimalFormat.format(mTempPriceSell * mTempDiscount / 10));
					}
					else if(mTempDiscount == 0){
						mEtPriceByDiscount.setText(mDecimalFormat.format(mTempPriceSell));
					}
				}
			}
			catch(Exception e) {
				mTempPriceSell = 0;
				e.printStackTrace();
			}
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	
	private TextWatcher mTextWatcherForDiscount = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			try{
				double discount = Double.valueOf(s.toString());
				if(discount < 0) {
					mEtDiscount.setText(String.valueOf(Math.abs(discount)));
				}
				else if(discount > 10) {
					mEtDiscount.setText(String.valueOf(10));
				}
				else {
					mTempDiscount = discount;
					
					if(mTempDiscount > 0) {
						mEtPriceByDiscount.setText(mDecimalFormat.format(mTempPriceSell * mTempDiscount / 10));
					}
					else if(mTempDiscount == 0){
						mEtPriceByDiscount.setText(mDecimalFormat.format(mTempPriceSell));
					}
				}
			}
			catch(Exception e) {
				mTempDiscount = 0;
				e.printStackTrace();
			}
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	
	private TextWatcher mTextWatcherForStore = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			try{
				double store = Double.valueOf(s.toString());
				if(store < 0) {
					mEtStore.setText(String.valueOf(Math.abs(store)));
				}
				else {
					mTempStore = store;
				}
			}
			catch(Exception e) {
				mTempStore = 0;
				e.printStackTrace();
			}
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	
	private TextWatcher mTextWatcherForWeightableStep = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			try{
				double weightStep = Double.valueOf(s.toString());
				if(weightStep < 0) {
					mEtWeightStep.setText(String.valueOf(Math.abs(weightStep)));
				}
				else {
					mTempWeightStep = weightStep;
				}
			}
			catch(Exception e) {
				mTempWeightStep = 0;
				e.printStackTrace();
			}
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	
	private OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(buttonView.equals(mCbWeightableYes)) {
				if(isChecked) {
					mCbWeightableYes.setEnabled(false);
					mCbWeightableNo.setEnabled(true);
					mCbWeightableNo.setChecked(false);
					mViewWeightStep.setVisibility(View.VISIBLE);
					mViewWeightUnit.setVisibility(View.VISIBLE);
					mTempWeightable = true;
					Log.d("CEXX", "CEXX => onCheckedChanged => mTempWeightable : " + mTempWeightable);
				}
			}
			else if(buttonView.equals(mCbWeightableNo)) {
				if(isChecked) {
					mCbWeightableNo.setEnabled(false);
					mCbWeightableYes.setEnabled(true);
					mCbWeightableYes.setChecked(false);
					mViewWeightStep.setVisibility(View.GONE);
					mViewWeightUnit.setVisibility(View.GONE);
					mTempWeightable = false;
					Log.d("CEXX", "CEXX => onCheckedChanged => mTempWeightable : " + mTempWeightable);
				}
			}
		}
	};
	
	public enum ModifyType {
		ModifyTypeOnShelf,
		ModifyTypeOffShelf,
		ModifyTypeDelete,
		ModifyTypeSave
	}
	
	public interface ModiyResultListener {
		public void modifyResult(boolean successful, ModifyType modifyType, AntGoods antGoods);
	}
	
	private ModiyResultListener mModiyResultListener;
	
	public GoodsHandleDialog(Context context, AntGoods antGoods, 
			List<GoodsKindBase> goodsKindBaseList, 
			GoodsKind goodsKind,
			AntGoodsStyles goodsStyles,
			ModiyResultListener listener) {
		super(context, R.style.choose_date_dialog_style);
		setContentView(R.layout.goods_handle_dialog);
		mContext = context;
		mAntGoods = antGoods;
		mGoodsKindBaseList = goodsKindBaseList;
		mCurrentGoodsKind = goodsKind;
		mCurrentGoodsStyles = goodsStyles;
		mModiyResultListener = listener;
		mDecimalFormat = new DecimalFormat("#########.##");
		
		mIvImage = (ImageView)findViewById(R.id.goods_handle_dialog_pic);
		mBtnLeft = (Button)findViewById(R.id.goods_handle_dialog_bottom_btn_left);
		mBtnMiddle = (Button)findViewById(R.id.goods_handle_dialog_bottom_btn_middle);
		mBtnRight = (Button)findViewById(R.id.goods_handle_dialog_bottom_btn_right);
		mTvPromptShelf = (TextView)findViewById(R.id.goods_handle_dialog_prompt_shelf);
		mTvGoodsTitle = (TextView)findViewById(R.id.goods_handle_dialog_title);
		mTvGoodsDes = (TextView)findViewById(R.id.goods_handle_dialog_des);
		mEtPricePurchase = (EditText)findViewById(R.id.goods_handle_price_purchase_et);
		mEtPriceSell = (EditText)findViewById(R.id.goods_handle_price_sell_et);
		mEtDiscount = (EditText)findViewById(R.id.goods_handle_discount_et);
		mEtPriceByDiscount = (EditText)findViewById(R.id.goods_handle_price_by_discount_et);
		mEtStore = (EditText)findViewById(R.id.goods_handle_store_et);
		mEtSpecs = (EditText)findViewById(R.id.goods_handle_specs_et);
		mEtUnit = (EditText)findViewById(R.id.goods_handle_unit_et);
		mEtWeightUnit = (EditText)findViewById(R.id.goods_handle_weight_unit_et);
		mEtWeightStep = (EditText)findViewById(R.id.goods_handle_weight_step_et);
		mViewRecommend = findViewById(R.id.goods_handle_dialog_recommend);
		mViewWeightUnit = findViewById(R.id.goods_handle_weight_unit_rl);
		mViewWeightStep = findViewById(R.id.goods_handle_weight_step_rl);
		mCbWeightableYes = (CheckBox)findViewById(R.id.goods_handle_weightable_cb_yes);
		mCbWeightableNo = (CheckBox)findViewById(R.id.goods_handle_weightable_cb_no);
		mMSpinnerKind = (MSpinner)findViewById(R.id.goods_handle_mspinner_kind);
		mMSpinnerStyle = (MSpinner)findViewById(R.id.goods_handle_mspinner_style);
		
		mViewRecommend.setOnClickListener(this);
		mBtnLeft.setOnClickListener(this);
		mBtnMiddle.setOnClickListener(this);
		mBtnRight.setOnClickListener(this);
		mEtPricePurchase.addTextChangedListener(mTextWatcherForPricePurchase);
		mEtPriceSell.addTextChangedListener(mTextWatcherForPriceSell);
		mEtDiscount.addTextChangedListener(mTextWatcherForDiscount);
		mEtStore.addTextChangedListener(mTextWatcherForStore);
		mEtWeightStep.addTextChangedListener(mTextWatcherForWeightableStep);
		
		mMSpinnerKind.setTitleTextColor(mContext.getResources().getColor(android.R.color.black));
		mMSpinnerKind.setTitleGravity(Gravity.CENTER);
		mMSpinnerKind.setHint(mContext.getResources().getString(R.string.add_goods_byhand_choose_kind));
		mMSpinnerKind.setOnMSpinnerItemClickListener(this);
		mMSpinnerKind.setDisplayItemInTitle(true);
		
		mMSpinnerStyle.setTitleTextColor(mContext.getResources().getColor(android.R.color.black));
		mMSpinnerStyle.setTitleGravity(Gravity.CENTER);
		mMSpinnerStyle.setHint(mContext.getResources().getString(R.string.add_goods_byhand_choose_style));
		mMSpinnerStyle.setOnMSpinnerItemClickListener(this);
		mMSpinnerStyle.setDisplayItemInTitle(true);
		
		if(mAntGoods != null) {
			enabledSet(mAntGoods.isFromLib());
			ImageLoader.getInstance().displayImage(mAntGoods.getImageUrl(), mIvImage, AntHomeApplication.imageDisplayOptions);
			mTvGoodsDes.setText(mAntGoods.getDescription());
			
			if(mAntGoods.getIsOnShelf()) {
				mTvPromptShelf.setText(mContext.getResources().getString(R.string.goods_handle_prompt_on_shelf));
				mBtnLeft.setText(mContext.getResources().getString(R.string.goods_handle_off_shelf));
			}
			else {
				mTvPromptShelf.setText(mContext.getResources().getString(R.string.goods_handle_prompt_off_shelf));
				mBtnLeft.setText(mContext.getResources().getString(R.string.goods_handle_on_shelf));
			}
			
			if(mAntGoods.isRecommend()) {
				mViewRecommend.setBackgroundResource(R.drawable.bt_recommend_on);
			} else {
				mViewRecommend.setBackgroundResource(R.drawable.bt_recommend_off);
			}
			
			mEtPricePurchase.setText(mDecimalFormat.format(mAntGoods.getInPrice()));
			mEtPriceSell.setText(mDecimalFormat.format(mAntGoods.getPrice()));
			mEtDiscount.setText(mDecimalFormat.format(mAntGoods.getDiscount()));
			mEtPriceByDiscount.setText(mDecimalFormat.format(mAntGoods.getRealPrice()));
			mEtStore.setText(mDecimalFormat.format(mAntGoods.getStock()));
			mEtSpecs.setText(mAntGoods.getSpecs());
			mEtUnit.setText(mAntGoods.getUnit());
			mEtWeightUnit.setText(mAntGoods.getWeighUnit());
			mEtWeightStep.setText(String.valueOf(mAntGoods.getWeighStep()));
			if(mAntGoods.isFromLib()) {
				if(mAntGoods.isWeighAble()) {
					mCbWeightableYes.setChecked(true);
					mCbWeightableNo.setChecked(false);
				}
				else {
					mCbWeightableYes.setChecked(false);
					mCbWeightableNo.setChecked(true);
				}
			}
			else {
				mCbWeightableYes.setOnCheckedChangeListener(mOnCheckedChangeListener);
				mCbWeightableNo.setOnCheckedChangeListener(mOnCheckedChangeListener);
				
				if(mAntGoods.isWeighAble()) {
					mCbWeightableYes.setChecked(true);
					mCbWeightableNo.setChecked(false);
					mCbWeightableYes.setEnabled(false);
					mCbWeightableNo.setEnabled(true);
				}
				else {
					mCbWeightableYes.setChecked(false);
					mCbWeightableNo.setChecked(true);
					mCbWeightableYes.setEnabled(true);
					mCbWeightableNo.setEnabled(false);
				}
			}
//			mMSpinnerKind;
//			mMSpinnerStyle;
			
			// 记录修改前的值
			mOriginPricePurchase = mAntGoods.getInPrice();
			mOriginPriceSell = mAntGoods.getPrice();
			mOriginDiscount = mAntGoods.getDiscount();
			mOriginStore = mAntGoods.getStock();
			mOriginWeightStep = mAntGoods.getWeighStep();
			mOriginWeightable = mAntGoods.isWeighAble();
			mOriginSpecs = mAntGoods.getSpecs();
			mOriginUnit = mAntGoods.getUnit();
			mOriginWeightUnit = mAntGoods.getWeighUnit();
			mOriginRecommend = mAntGoods.isRecommend();
			// 设置临时变量
			mTempPricePurchase = mAntGoods.getInPrice();
			mTempPriceSell = mAntGoods.getPrice();
			mTempDiscount = mAntGoods.getDiscount();
			mTempStore = mAntGoods.getStock();
			mTempWeightStep = mAntGoods.getWeighStep();
			mTempWeightable = mAntGoods.isWeighAble();
			mTempRecommend = mAntGoods.isRecommend();
//			mTempSpecs = mAntGoods.getSpecs();
//			mTempUnit = mAntGoods.getUnit();
//			mTempWeightUnit = mAntGoods.getWeighUnit();
		}
	}
	
	/**
	 *  AntGoodsBase不允许修改的属性(从在线库中添加的商品，编辑商品时，只能编辑进价，售价和库存)
	 */
	private void enabledSet(boolean isFromLib) {
		if(isFromLib) {
			mCbWeightableNo.setEnabled(false);
			mCbWeightableYes.setEnabled(false);
			mEtDiscount.setEnabled(true);
			mEtPriceByDiscount.setEnabled(false);
			mEtSpecs.setEnabled(false);
			mEtUnit.setEnabled(false);
			mEtWeightUnit.setEnabled(false);
			mEtWeightStep.setEnabled(false);
			mMSpinnerKind.setEnabled(false);
			mMSpinnerStyle.setEnabled(false);
		}
		else {
			mCbWeightableYes.setEnabled(false);
			mCbWeightableNo.setEnabled(true);
			mEtDiscount.setEnabled(true);
			mEtPriceByDiscount.setEnabled(true);
			mEtSpecs.setEnabled(true);
			mEtUnit.setEnabled(true);
			mEtWeightUnit.setEnabled(true);
			mEtWeightStep.setEnabled(true);
			mMSpinnerKind.setEnabled(true);
			mMSpinnerStyle.setEnabled(true);
		}
	}
	
	@Override
	public void onClick(View v) {
		if(v.equals(mBtnLeft)) {
			// 上架或下架
			if(mAntGoods == null) {
				return;
			}
			
			if(mAntGoods.getIsOnShelf()) {
				this.attrModifyOffShelf();
			}
			else {
				this.attrModifyOnShelf();
			}
		}
		else if(v.equals(mBtnMiddle)) {
			// 删除
			this.attrModifyDelete();
		}
		else if(v.equals(mBtnRight)) {
			// 保存
			if(mAntGoods == null) {
				return;
			}
			
			this.attrModifySave();
		}
		else if(v.equals(mViewRecommend)) {
			if(mTempRecommend) {
				mTempRecommend = false;
				mViewRecommend.setBackgroundResource(R.drawable.bt_recommend_off);
			} else {
				mTempRecommend = true;
				mViewRecommend.setBackgroundResource(R.drawable.bt_recommend_on);
			}
		}
	}
	
	@Override
	public void onMSpinnerItemClick(MSpinner mspinner, int position,
			MSpinnerBean bean) {
		if(mspinner.equals(mMSpinnerKind)) {
			mMSpinnerBeanListStyle.clear();
			GoodsKindBase gkb = mGoodsKindBaseList.get(position);
			List<AntGoodsStylesBase> styleBaseList = gkb.getGoodsStylesList();
			if(styleBaseList != null) {
				for(AntGoodsStylesBase agsb : styleBaseList) {
					if(!TextUtils.isEmpty(agsb.getName())) {
						MSpinnerBean tempBean = new MSpinnerBean();
						tempBean.contentStr = agsb.getName();
						mMSpinnerBeanListStyle.add(tempBean);
					}
				}
				mMSpinnerStyle.setDropDownListData(mMSpinnerBeanListStyle);
			}
		}
	}
	
	private void attrModifyOnShelf() {
		if(!check(mAntGoods.isFromLib())) {
			return;
		}
		
		AlertDialog dialog = new AlertDialog.Builder(mContext)
				.setTitle(R.string.order_ensure_order_commit_prompt)
				.setMessage(R.string.goods_handle_attr_modify_prompt_onshelf)
				.setNegativeButton(R.string.sure,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								mAntGoods.setInPrice(mTempPricePurchase);
								mAntGoods.setPrice(mTempPriceSell);
								mAntGoods.setDiscount(mTempDiscount);
								mAntGoods.setStock(mTempStore);
								mAntGoods.setWeighStep(mTempWeightStep);
								mAntGoods.setWeighAble(mTempWeightable);
								mAntGoods.setIsRecommend(mTempRecommend);
								mAntGoods.setSpecs(mEtSpecs.getText().toString());
								mAntGoods.setUnit(mEtUnit.getText().toString());
								mAntGoods.setWeighUnit(mEtWeightUnit.getText().toString());
								mAntGoods.setShelfState(true);
								mAntGoods.saveInBackground(new SaveCallback() {
									@Override
									public void done(AVException arg0) {
										if (arg0 == null) {
											Toast.makeText(
													mContext,
													R.string.goods_handle_attr_modify_prompt_successful,
													Toast.LENGTH_SHORT).show();
											if (mModiyResultListener != null) {
												mModiyResultListener
														.modifyResult(
																true,
																ModifyType.ModifyTypeOnShelf,
																mAntGoods);
											}
										} else {
											Toast.makeText(
													mContext,
													R.string.goods_handle_attr_modify_prompt_failed,
													Toast.LENGTH_SHORT).show();
											mAntGoods.setInPrice(mOriginPricePurchase);
											mAntGoods.setPrice(mOriginPriceSell);
											mAntGoods.setDiscount(mOriginDiscount);
											mAntGoods.setStock(mOriginStore);
											mAntGoods.setWeighStep(mOriginWeightStep);
											mAntGoods.setWeighAble(mOriginWeightable);
											mAntGoods.setIsRecommend(mOriginRecommend);
											mAntGoods.setSpecs(mOriginSpecs);
											mAntGoods.setUnit(mOriginUnit);
											mAntGoods.setWeighUnit(mOriginWeightUnit);
											mAntGoods.setShelfState(false);
											if (mModiyResultListener != null) {
												mModiyResultListener
														.modifyResult(
																false,
																ModifyType.ModifyTypeOnShelf,
																mAntGoods);
											}
										}
										GoodsHandleDialog.this.dismiss();
									}
								});
							}
						})
				.setPositiveButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						}).create();
		dialog.show();
	}

	private void attrModifyOffShelf() {
		if(!check(mAntGoods.isFromLib())) {
			return;
		}
		
		AlertDialog dialog = new AlertDialog.Builder(mContext)
				.setTitle(R.string.order_ensure_order_commit_prompt)
				.setMessage(R.string.goods_handle_attr_modify_prompt_offshelf)
				.setNegativeButton(R.string.sure,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								mAntGoods.setInPrice(mTempPricePurchase);
								mAntGoods.setPrice(mTempPriceSell);
								mAntGoods.setDiscount(mTempDiscount);
								mAntGoods.setStock(mTempStore);
								mAntGoods.setWeighStep(mTempWeightStep);
								mAntGoods.setWeighAble(mTempWeightable);
								mAntGoods.setIsRecommend(mTempRecommend);
								mAntGoods.setSpecs(mEtSpecs.getText().toString());
								mAntGoods.setUnit(mEtUnit.getText().toString());
								mAntGoods.setWeighUnit(mEtWeightUnit.getText().toString());
								mAntGoods.setShelfState(false);
								mAntGoods.saveInBackground(new SaveCallback() {
									@Override
									public void done(AVException arg0) {
										if (arg0 == null) {
											Toast.makeText(
													mContext,
													R.string.goods_handle_attr_modify_prompt_successful,
													Toast.LENGTH_SHORT).show();
											if (mModiyResultListener != null) {
												mModiyResultListener
														.modifyResult(
																true,
																ModifyType.ModifyTypeOffShelf,
																mAntGoods);
											}
										} else {
											Toast.makeText(
													mContext,
													R.string.goods_handle_attr_modify_prompt_failed,
													Toast.LENGTH_SHORT).show();
											mAntGoods.setInPrice(mOriginPricePurchase);
											mAntGoods.setPrice(mOriginPriceSell);
											mAntGoods.setDiscount(mOriginDiscount);
											mAntGoods.setStock(mOriginStore);
											mAntGoods.setWeighStep(mOriginWeightStep);
											mAntGoods.setWeighAble(mOriginWeightable);
											mAntGoods.setIsRecommend(mOriginRecommend);
											mAntGoods.setSpecs(mOriginSpecs);
											mAntGoods.setUnit(mOriginUnit);
											mAntGoods.setWeighUnit(mOriginWeightUnit);
											mAntGoods.setShelfState(true);
											if (mModiyResultListener != null) {
												mModiyResultListener
														.modifyResult(
																false,
																ModifyType.ModifyTypeOffShelf,
																mAntGoods);
											}
										}
										GoodsHandleDialog.this.dismiss();
									}
								});
							}
						})
				.setPositiveButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						}).create();
		dialog.show();
	}

	private void attrModifyDelete() {
		AlertDialog dialog = new AlertDialog.Builder(mContext)
				.setTitle(R.string.order_ensure_order_commit_prompt)
				.setMessage(R.string.goods_handle_attr_modify_prompt_delete)
				.setNegativeButton(R.string.sure,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								RetrieveDataAsyn.getInstance().deleteGoods(
										mAntGoods, mCurrentGoodsKind, mCurrentGoodsStyles, new RetrieveDataListener() {
											@Override
											public void dataRetrieved(
													int requestId, Object data) {
												handleDeleteResult(data);
											}
										});
							}
						})
				.setPositiveButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						}).create();
		dialog.show();
	}
	
	private void handleDeleteResult(Object data) {
		if (data == null) {
			Toast.makeText(mContext,
					R.string.goods_handle_attr_modify_prompt_successful,
					Toast.LENGTH_SHORT).show();
			if (mModiyResultListener != null) {
				mModiyResultListener.modifyResult(true,
						ModifyType.ModifyTypeDelete, mAntGoods);
			}

			if (AntHomeApplication.sCurrentGoodsCount > 0) {
				AntHomeApplication.sCurrentGoodsCount--;
			}
		} else if (data instanceof AVException) {
			Toast.makeText(mContext,
					R.string.goods_handle_attr_modify_prompt_failed,
					Toast.LENGTH_SHORT).show();
			if (mModiyResultListener != null) {
				mModiyResultListener.modifyResult(false,
						ModifyType.ModifyTypeDelete, mAntGoods);
			}
		} else if (data instanceof GoodsKind) {
			Log.d("CEXX", "CEXX => handleDeleteResult => data instanceof GoodsKind");
		} else if (data instanceof AntGoodsStyles) {
			Log.d("CEXX", "CEXX => handleDeleteResult => data instanceof AntGoodsStyles");
		}
		
		GoodsHandleDialog.this.dismiss();
	}

	private void attrModifySave() {
		if(!check(mAntGoods.isFromLib())) {
			return;
		}
		
		AlertDialog dialog = new AlertDialog.Builder(mContext)
				.setTitle(R.string.order_ensure_order_commit_prompt)
				.setMessage(R.string.goods_handle_attr_modify_prompt_save)
				.setNegativeButton(R.string.sure,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								mAntGoods.setInPrice(mTempPricePurchase);
								mAntGoods.setPrice(mTempPriceSell);
								mAntGoods.setDiscount(mTempDiscount);
								mAntGoods.setStock(mTempStore);
								mAntGoods.setWeighStep(mTempWeightStep);
								mAntGoods.setWeighAble(mTempWeightable);
								mAntGoods.setIsRecommend(mTempRecommend);
								mAntGoods.setSpecs(mEtSpecs.getText().toString());
								mAntGoods.setUnit(mEtUnit.getText().toString());
								mAntGoods.setWeighUnit(mEtWeightUnit.getText().toString());
								mAntGoods.saveInBackground(new SaveCallback() {
									@Override
									public void done(AVException arg0) {
										if (arg0 == null) {
											Toast.makeText(
													mContext,
													R.string.goods_handle_attr_modify_prompt_successful,
													Toast.LENGTH_SHORT).show();
											if (mModiyResultListener != null) {
												mModiyResultListener
														.modifyResult(
																true,
																ModifyType.ModifyTypeSave,
																mAntGoods);
											}
										} else {
											Toast.makeText(
													mContext,
													R.string.goods_handle_attr_modify_prompt_failed,
													Toast.LENGTH_SHORT).show();
											mAntGoods.setInPrice(mOriginPricePurchase);
											mAntGoods.setPrice(mOriginPriceSell);
											mAntGoods.setDiscount(mOriginDiscount);
											mAntGoods.setStock(mOriginStore);
											mAntGoods.setWeighStep(mOriginWeightStep);
											mAntGoods.setWeighAble(mOriginWeightable);
											mAntGoods.setIsRecommend(mOriginRecommend);
											mAntGoods.setSpecs(mOriginSpecs);
											mAntGoods.setUnit(mOriginUnit);
											mAntGoods.setWeighUnit(mOriginWeightUnit);
											if (mModiyResultListener != null) {
												mModiyResultListener
														.modifyResult(
																false,
																ModifyType.ModifyTypeSave,
																mAntGoods);
											}
										}
										GoodsHandleDialog.this.dismiss();
									}
								});
							}
						})
				.setPositiveButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						}).create();
		dialog.show();
	}
	
	private boolean check(boolean isFromLib) {
    	if(!isFromLib) {
//    		if(mCustomImageUri == null) {
//    			// 检查商品图片
//    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_pic));
//    			return;
//    		}
//    		else if(TextUtils.isEmpty(mEtDescription.getText().toString())) {
//    			// 检查商品描述
//    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_des));
//    			return;
//    		}
//    		else if(TextUtils.isEmpty(mMSpinnerKind.getText())) {
//    			// 检查商品大类
//    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_kind));
//    			return;
//    		}
//    		else if(TextUtils.isEmpty(mMSpinnerStyle.getText())) {
//    			// 检查商品小类
//    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_style));
//    			return;
//    		}
//    		else 
    		if(TextUtils.isEmpty(mEtPricePurchase.getText().toString())) {
    			// 检查商品进价
    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_purchase));
    			return false;
    		}
    		else if(TextUtils.isEmpty(mEtPriceSell.getText().toString())) {
    			// 检查商品售价
    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_sell));
    			return false;
    		}
    		else if(TextUtils.isEmpty(mEtStore.getText().toString())) {
    			// 检查商品库存
    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_store));
    			return false;
    		}
//    		else if(TextUtils.isEmpty(mEtSpecs.getText().toString())) {
//    			// 检查商品规格
//    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_specs));
//    			return false;
//    		}
    		else if(TextUtils.isEmpty(mEtUnit.getText().toString())) {
    			// 检查商品计价单位
    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_unit));
    			return false;
    		}
    		else {
    			return true;
    		}
    	}
    	else {
    		if(TextUtils.isEmpty(mEtPriceSell.getText().toString())) {
    			// 检查商品售价
    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_sell));
    			return false;
    		}
    		else if(TextUtils.isEmpty(mEtStore.getText().toString())) {
    			// 检查商品库存
    			UtilTools.showMyToast(mContext, mContext.getResources().getString(R.string.add_goods_check_prompt_store));
    			return false;
    		}
    		else {
    			return true;
    		}
    	}
	}
}















